/*
 * Copyright (C) 2011 bus.fm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.busfm.util;

import java.io.File;
import java.io.FileWriter;

import android.content.Context;
import android.os.Environment;

/**
 * <p>
 * Title:FileUtil
 * </p>
 * <p>
 * Description: FileUtil
 * </p>
 * <p>
 * Copyright (c) 2011 www.bus.fm Inc. All rights reserved.
 * </p>
 * <p>
 * Company: bus.fm
 * </p>
 * 
 * 
 * @author jingguo0@gmail.com
 * 
 */

public class FileUtil {
    public static final String SEPARATOR = File.separator;
    public static final String DOT = ".";
    private static final String COVER_DIR = "cover";
    private static final String DATA_DIR = "data";
    public static final String EXTERNALSTORAGE = Environment.getExternalStorageDirectory().getPath();

    private FileUtil() {

    }

    public static boolean isMounted() {
        boolean result = false;

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageDirectory())
                || Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            result = true;
        }

        return result;
    }

    public static String getStoragePath() {
        return EXTERNALSTORAGE;
    }

    public static String getStorageDir() {
        String result = getStoragePath();

        if (!Utilities.isEmpty(result)) {
            result += SEPARATOR + Constants.AppliactionName;
            createDir(result);
        }

        return result;
    }

    public static String getCoverDir() {
        String result = getStorageDir();

        if (!Utilities.isEmpty(result)) {
            result += SEPARATOR + COVER_DIR;
            createDir(result);
        }

        return result;
    }

    public static String getDataDir() {
        String result = getStorageDir();

        if (!Utilities.isEmpty(result)) {
            result += SEPARATOR + DATA_DIR;
            createDir(result);
        }

        return result;
    }

    public static File getCacheFile(Context context) {
        return context.getCacheDir();
    }

    public static boolean isExisted(String path) {
        boolean result = false;

        if (!Utilities.isEmpty(path)) {
            File file = new File(path);
            result = file.exists();
        }

        return result;
    }

    public static void createDir(String dir) {
        File file = new File(dir);
        createDir(file);
    }

    public static void createDir(File file) {
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 鍒涘缓鏂囦欢锛屽苟寰�枃浠朵腑鍐欏唴瀹�
     */
    public static void writeFile(String path, String content, boolean append) {
        try {
            File file = new File(path);

            if (!file.exists()) {
                file.createNewFile();
                file = new File(path); // 閲嶆柊瀹炰緥鍖�
            }

            FileWriter fw = new FileWriter(file, append);

            if (!Utilities.isEmpty(content)) {
                fw.write(content);
                fw.flush();
            }

            fw.close();
        } catch (Exception e) {
            LogUtil.e(Constants.TAG, e.getMessage());
        }
    }

    /**
     * 鍒犻櫎鏂囦欢澶�
     * 
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 鍒犻櫎瀹岄噷闈㈡墍鏈夊唴瀹�
            String filePath = folderPath;
            File file = new File(filePath);
            file.delete(); // 鍒犻櫎绌烘枃浠跺す
        } catch (Exception e) {
            LogUtil.e(Constants.TAG, e.getMessage());
        }
    }

    /**
     * 鍒犻櫎鎸囧畾鏂囦欢澶逛笅鎵�湁鏂囦欢
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);

        if (!file.exists()) {
            return flag;
        }

        if (!file.isDirectory()) {
            return flag;
        }

        String[] list = file.list();
        File temp = null;

        for (int i = 0; i < list.length; i++) {

            if (path.endsWith(SEPARATOR)) {
                temp = new File(path + list[i]);
            } else {
                temp = new File(path + SEPARATOR + list[i]);
            }

            if (temp.isFile()) {
                temp.delete();
            }

            if (temp.isDirectory()) {
                delAllFile(path + SEPARATOR + list[i]);// 鍏堝垹闄ゆ枃浠跺す閲岄潰鐨勬枃浠�
                delFolder(path + SEPARATOR + list[i]);// 鍐嶅垹闄ょ┖鏂囦欢澶�
                flag = true;
            }

        }

        return flag;
    }

    public static boolean deleteFile(String path) {
        boolean isDeleted = false;
        File file = new File(path);

        if (file.isFile()) {
            isDeleted = file.delete();
        }

        return isDeleted;
    }

    public static void clearImageCache() {
        File file = new File(FileUtil.getCoverDir());
        if (file.exists()) {
            deleteDir(file);
        }
    }

    public static void deleteDir(File dir) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                deleteDir(file);
            }
            file.delete();
        }
    }

}
